﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Commodity;

namespace FwProject.Manager
{
    public class CommodityCollectionManager : IDomainService
    {
        private readonly IRepository<CommodityCollection, Guid> _commodityCollectionRepository;

        public CommodityCollectionManager(IRepository<CommodityCollection, Guid> commodityCollectionRepository)
        {
            _commodityCollectionRepository = commodityCollectionRepository;
        }
        /// <summary>
        /// 添加一个收藏
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateCommodityCollection(CommodityCollection model)
        {
            var m = _commodityCollectionRepository.GetAll().Where(o => o.ShopproductId == model.ShopproductId && o.UserId==model.UserId).FirstOrDefault();
            Guid guid = Guid.Empty;
            if (m != null)
            {
                m.isActive = !m.isActive;
                guid = model.Id;
            }
            else {
                guid= await _commodityCollectionRepository.InsertAndGetIdAsync(model);
            }
            return guid;
        }
        /// <summary>
        /// 修改样一个收藏
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateCommodityCollection(CommodityCollection model)
        {

            await _commodityCollectionRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除一个收藏
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteCommodityCollection(CommodityCollection model)
        {
            await _commodityCollectionRepository.DeleteAsync(model);
        }
    }
}
